# -*- coding: utf-8 -*-
# 利用map和reduce编写一个str2float函数，把字符串'123.456'转换成浮点数123.456：
from functools import reduce

def str2float(s):
    DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}

    def char2num(s):
        if s == '.': return -1
        return DIGITS[s]
    def tofloat(x,y):
        # print(x,y)
        if y == -1:
            x += 0.0
            return x
        elif type(x) != float:
            return x*10+y
        else:
            i = int(x)
            f = x-i
            f = (f*10**(len(str(f))-1)+y)/10**(len(str(f))-1)
            print(f)
            return i+f
            
    return reduce(tofloat, map(char2num, s))
print('str2float(\'123.456\') =', str2float('123.456'))
if abs(str2float('123.456') - 123.456) < 0.00001:
    print('测试成功!')
else:
    print('测试失败!')